/**
 * Crear un programa que resuelva la ecuación cuadrática 
 * tipo ax2 + bx + c = 0.
 */
package com.virglinio.resolvente;
/*
 *  Principal.java is part of resolvente-cuadratica
 *
 *  resolvente-cuadratica is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  resolvente-cuadratica is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with resolvente-cuadratica.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 * 
 * @author Mario Fernando Virglinio
 * @see <a href="http://www.virglinio.com.ar">http://www.virglinio.com.ar</a>
 *
 */
public class Principal {

	private JFrame frmEcuacinCuadrtica;
	private JTextField txtParametroA;
	private JTextField txtParametroB;
	private JTextField txtParametroC;
	private JLabel lblX1;
	private JLabel lblX2;
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Principal window = new Principal();
					window.frmEcuacinCuadrtica.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Principal() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmEcuacinCuadrtica = new JFrame();
		frmEcuacinCuadrtica.setTitle("Ecuación cuadrática");
		frmEcuacinCuadrtica.setBounds(100, 100, 450, 300);
		frmEcuacinCuadrtica.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmEcuacinCuadrtica.getContentPane().setLayout(null);
		
		JLabel lblParmetrosAB = new JLabel("Parámetros a, b y c para ecuación tipo a*x² + b*x + c = 0");
		lblParmetrosAB.setBounds(12, 25, 418, 15);
		frmEcuacinCuadrtica.getContentPane().add(lblParmetrosAB);
		
		JLabel lblValorParmetroA = new JLabel("Valor parámetro a");
		lblValorParmetroA.setBounds(54, 65, 144, 15);
		frmEcuacinCuadrtica.getContentPane().add(lblValorParmetroA);
		
		JLabel lblValorParmetroB = new JLabel("Valor parámetro b");
		lblValorParmetroB.setBounds(54, 92, 144, 15);
		frmEcuacinCuadrtica.getContentPane().add(lblValorParmetroB);
		
		JLabel lblValorParmetroC = new JLabel("Valor parámetro c");
		lblValorParmetroC.setBounds(54, 119, 144, 15);
		frmEcuacinCuadrtica.getContentPane().add(lblValorParmetroC);
		
		txtParametroA = new JTextField();
		txtParametroA.setBounds(230, 63, 114, 19);
		frmEcuacinCuadrtica.getContentPane().add(txtParametroA);
		txtParametroA.setColumns(10);
		
		txtParametroB = new JTextField();
		txtParametroB.setBounds(230, 90, 114, 19);
		frmEcuacinCuadrtica.getContentPane().add(txtParametroB);
		txtParametroB.setColumns(10);
		
		txtParametroC = new JTextField();
		txtParametroC.setBounds(230, 117, 114, 19);
		frmEcuacinCuadrtica.getContentPane().add(txtParametroC);
		txtParametroC.setColumns(10);
		
		JButton btnCalcularX = new JButton("Calcular x");
		btnCalcularX.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {				
				
				double parametroA, parametroB, parametroC, x1, x2;
							
				parametroA = Double.parseDouble(txtParametroA.getText());				
				parametroB = Double.parseDouble(txtParametroB.getText());
				parametroC = Double.parseDouble(txtParametroC.getText());
				
				x1 = (-parametroB + Math.sqrt(Math.pow(parametroB, 2.0) - (4 * parametroA * parametroC))) / (2 * parametroA);
				x2 = (-parametroB - Math.sqrt(Math.pow(parametroB, 2.0) - (4 * parametroA * parametroC))) / (2 * parametroA);
				
				lblX1.setText("x1 = " + Double.toString(x1));
				lblX2.setText("x2 = " + Double.toString(x2));
			}
		});
		btnCalcularX.setBounds(146, 167, 117, 25);
		frmEcuacinCuadrtica.getContentPane().add(btnCalcularX);
		
		lblX1 = new JLabel("New label");
		lblX1.setBounds(54, 210, 376, 15);
		frmEcuacinCuadrtica.getContentPane().add(lblX1);
		
		lblX2 = new JLabel("New label");
		lblX2.setBounds(54, 237, 376, 15);
		frmEcuacinCuadrtica.getContentPane().add(lblX2);
	}
}
